package com.example.basic.excels;

import com.example.basic.build_id.ClientIdBuild;
import com.example.basic.utils.DateUtils;
import com.google.gson.Gson;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ExcelWirter {



    public static void export(List<Log> data, String[] header) {
        String[] fieldNames = fieldName(User.class);
        System.out.println("data size = " + data.size());

//        ArrayList<String> datas = new ArrayList<>();
        int pageSize = 1;
        if (data.size()/60000 != 0) {
            pageSize = data.size()/60000 + 1;
        }



        Workbook wb = new HSSFWorkbook();
        int rowSize = 0;
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(rowSize);
        for (int i = 0; i < header.length; i++) {
            row.createCell(i).setCellValue(header[i]);
        }


        try {
            for (int x = 0; x < data.size(); x++) {
                rowSize = 1;
                Row rowNew = sheet.createRow(rowSize + x);
                Log log = data.get(x);
                rowNew.createCell(0).setCellValue(DateUtils.timestamp2DateStr(log.timestamp + ""));
                rowNew.createCell(1).setCellValue(ClientIdBuild.translateClientId(log.version));
                rowNew.createCell(2).setCellValue(log.client_ip);
                rowNew.createCell(3).setCellValue(log.ctype);
                rowNew.createCell(4).setCellValue(unionid2Str(log.unionid));
                rowNew.createCell(5).setCellValue(log.custom_data);
//                datas.add(log.custom_data);
                rowNew.createCell(6).setCellValue(log.os_version);
                rowNew.createCell(7).setCellValue(log.arch);
                rowNew.createCell(8).setCellValue(log.os_type);
                rowNew.createCell(9).setCellValue(log.pcid);
                rowNew.createCell(10).setCellValue(log.sub_module);
                rowNew.createCell(11).setCellValue(log.event_id);
                rowNew.createCell(12).setCellValue(DateUtils.timestamp2DateStr(log.c_timestamp + ""));
                rowNew.createCell(13).setCellValue(log.business);
                rowNew.createCell(14).setCellValue(log.appid);
            }
        } catch (Exception e) {

        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("result.xls");
            wb.write(outputStream);
        } catch (Exception e) {

        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (Exception e) {

            }
            try{
                if(wb != null){
                    wb.close();
                }
            } catch (Exception e){

            }
        }

//        File keysTxt = new File("keysTxt.txt");
//        if (!keysTxt.exists()) {
//            try {
//                keysTxt.createNewFile();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//        Gson gson = new Gson();
//
//        try {
//            BufferedWriter writer = new BufferedWriter(new FileWriter(keysTxt));
//            for (String s : datas) {
//                DataInfo dataInfo;
//                try {
//                    dataInfo = gson.fromJson(s, DataInfo.class);
//                } catch (Exception e) {
//                    dataInfo = null;
//                }
//
//                if (dataInfo != null && "顶部搜索栏".equals(dataInfo.getPosition())) {
//                    writer.write(dataInfo.getKeyword() + "\n");
//                }
//            }
//            writer.flush();
//            writer.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    //测试
    public static final short UNION_ID_TEST = 100;
    //官网
    public static final short UNION_ID_OFFICIAL = 1;
    //腾讯应用宝
    public static final short UNION_ID_YINGYONGBAO = 2;
    //华为应用商店
    public static final short UNION_ID_HUAWEI = 3;
    //百度手机助手
    public static final short UNION_ID_BAIDU = 4;
    //vivo应用商店
    public static final short UNION_ID_VIVO = 5;
    //oppo应用商店
    public static final short UNION_ID_OPPO = 6;
    //小米应用商店
    public static final short UNION_ID_XIAOMI = 7;
    //阿里应用商店
    public static final short UNION_ID_ALI = 8;
    //360手机助手
    public static final short UNION_ID_360 = 9;
    //中关村
    public static final short UNION_ID_ZGC = 10;
    //联想
    public static final short UNION_ID_LENOVO = 11;
    //上海金总
    public static final short UNION_ID_SHJZ = 12;
    //太平洋
    public static final short UNION_ID_TPY = 13;

    private static String unionid2Str(int unionid) {
        String result = "";
        switch (unionid) {
            case UNION_ID_TEST: result = "oapm";break;
            case UNION_ID_OFFICIAL: result = "official";break;
            case UNION_ID_YINGYONGBAO: result = "yingyongbao";break;
            case UNION_ID_HUAWEI: result = "huawei";break;
            case UNION_ID_BAIDU: result = "baidu";break;
            case UNION_ID_VIVO: result = "vivo";break;
            case UNION_ID_OPPO: result = "oppo";break;
            case UNION_ID_XIAOMI: result = "xiaomi";break;
            case UNION_ID_ALI: result = "ali";break;
            case UNION_ID_360: result = "360";break;
            case UNION_ID_ZGC: result = "zgc";break;
            case UNION_ID_LENOVO: result = "lenovo";break;
            case UNION_ID_SHJZ: result = "shjz";break;
            case UNION_ID_TPY: result = "tpy";break;
            default:result = "oapm";break;
        }
        return result;
    }

    private static String[] fieldName(Class clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        String[] fieldNames = new String[declaredFields.length];
        for (int i = 0; i < declaredFields.length; i++) {
            fieldNames[i] = declaredFields[i].getName(); //通过反射获取属性名
        }
        return fieldNames;
    }

}
